import QtQuick 1.1
import com.nokia.meego 1.0
import "constants.js" as UI

Item {
    id: listItem
    clip: true

    signal clicked
    property alias pressed: mouseArea.pressed
    property bool folder: false
    //0: folder, 1: txt, 2:umd
    property int iconType: 0

    property string title: model.title ? model.title : ""
    //size of file, unit is byte
    property int size: 0
    property url iconSource: iconUrl()
    property int titleSize: UI.LIST_TILE_SIZE
    property int titleWeight: Font.Bold
    property color titleColor: theme.inverted ? UI.LIST_TITLE_COLOR_INVERTED : UI.LIST_TITLE_COLOR

    property int subtitleSize: UI.LIST_SUBTILE_SIZE
    property int subtitleWeight: Font.Light
    property color subtitleColor: theme.inverted ? UI.LIST_SUBTITLE_COLOR_INVERTED : UI.LIST_SUBTITLE_COLOR

    height: UI.LIST_ITEM_HEIGHT
    width: parent.width

    function toFixed(value) {
        var number = new Number(value)
        return number.toFixed(2)
    }

    function iconUrl() {
        if (iconType ===0 ) {
            return "../images/folder.png"
        }
        else if (iconType === 1) {
            return "../images/txt_file.png"
        }
        else if (iconType === 2) {
            return "../images/umd_file.png"
        }
    }

    function sizeStr(size) {
        if (size >=0 && size < 1000) {
            //Byte
            return size + "Byte"
        }
        else if (size >= 1000 && size < 1000*1000) {
            //KB
            return toFixed(size/1000)+"KB"
        }
        else if (size >= 1000*1000 && size < 1000*1000*1000) {
            //MB
            return toFixed(size/(1000*1000)) + "MB"
        }
        else if (size >= 1000*1000*1000) {
            return toFixed(size/(1000*1000*1000)) + "GB"
        }
        else {
            return "Invalid size"
        }
    }

    Rectangle {
        id: background
        anchors.fill: parent
        // Fill page porders
        visible: mouseArea.pressed
        color: "grey"
    }

    Item {
        anchors.fill: parent
        Image {
            id: image
            smooth: true
            anchors.verticalCenter: parent.verticalCenter
            anchors.left: parent.left
            width: Math.min(parent.width, parent.height)
            height: width
            source: listItem.iconSource
        }

        Label {
            id: mainText
            anchors.verticalCenter: parent.verticalCenter
            anchors.left: image.right
            anchors.right: sizeText.left
            horizontalAlignment: Text.AlignLeft
//            height: parent.height
//            clip: true
            elide: Text.ElideRight
            text: listItem.title
            textFormat: Text.PlainText
            font.weight: listItem.titleWeight
            font.pixelSize: listItem.titleSize
            color: listItem.titleColor
            wrapMode: Text.Wrap
        }

        Label {
            id: sizeText
//            clip: true
//            height: parent.height
            anchors.verticalCenter: parent.verticalCenter
            anchors.right: parent.right
            horizontalAlignment: Text.AlignRight
            text: sizeStr(listItem.size)
            font.weight: listItem.subtitleWeight
            font.pixelSize: listItem.subtitleSize
            color: listItem.subtitleColor
            visible: folder === false
            wrapMode: Text.Wrap
        }
    }
    MouseArea {
        id: mouseArea;
        anchors.fill: parent
        onClicked: {
            listItem.clicked();
        }
    }
}
